package com.ruoyi.hatha.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.hatha.domain.PKcb;
import com.ruoyi.hatha.domain.PMyvip;
import com.ruoyi.hatha.domain.PYuyue;
import com.ruoyi.hatha.service.IPKcbService;
import com.ruoyi.hatha.service.IPMyvipService;
import com.ruoyi.hatha.service.IPYuyueService;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.hatha.domain.PYkecheng;
import com.ruoyi.hatha.service.IPYkechengService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 课程列Controller
 * 
 * @author ruoyi
 * @date 2024-04-15
 */
@RestController
@RequestMapping("/hatha/ykecheng")
public class PYkechengController extends BaseController
{
    @Autowired
    private IPYkechengService pYkechengService;
    @Autowired
    private IPKcbService kcbService;
    @Autowired
    private IPYuyueService yuyueService;
    @Autowired
    private ISysUserService sysUserService;
    @Autowired
    private IPMyvipService myvipService;

    /**
     * 查询课程列列表
     */
    @PreAuthorize("@ss.hasPermi('hatha:ykecheng:list')")
    @GetMapping("/list")
    public TableDataInfo list(PYkecheng pYkecheng)
    {
        startPage();
        List<PYkecheng> list = pYkechengService.selectPYkechengList(pYkecheng);
        return getDataTable(list);
    }


    @GetMapping("/quxiao/{id}")
    public AjaxResult quxiao(@PathVariable("id") Long id)
    {

        PYkecheng py = pYkechengService.selectPYkechengById(id);
        PKcb kcb = kcbService.selectPKcbById(py.getBid());

        PYuyue pyy = new PYuyue();
        pyy.setYkid(id);
        pyy.setYytime(kcb.getDateStr());
        pyy.setState(0);

        List<PYuyue> lpyy = yuyueService.selectPYuyueList(pyy);

        for(PYuyue pyuyue : lpyy){
            if(pyuyue.getVid()!=null){
                Long vid = pyuyue.getVid();
                PMyvip pm = myvipService.selectPMyvipById(vid);
                if("4".equals(pm.getType())){
                    pm.setSycs(pm.getSycs()+1);
                    myvipService.updatePMyvip(pm);
                }
            }
        }

        pyy.setState(1);
        yuyueService.quxiao(pyy);
        return AjaxResult.success();
    }

    @GetMapping("/yyinfo/{id}")
    public AjaxResult yyinfo(@PathVariable("id") Long id)
    {

        PYuyue pyy = new PYuyue();
        pyy.setYkid(id);
        pyy.setState(0);
        List<PYuyue> lpyy = yuyueService.selectPYuyueList(pyy);

        for (int i=0;i<lpyy.size();i++){
            Long uid = lpyy.get(i).getUid();
            SysUser su = sysUserService.selectUserById(uid);
            lpyy.get(i).setUser(su);
        }

        return AjaxResult.success(lpyy);
    }

    /**
     * 导出课程列列表
     */
    @PreAuthorize("@ss.hasPermi('hatha:ykecheng:export')")
    @Log(title = "课程列", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PYkecheng pYkecheng)
    {
        List<PYkecheng> list = pYkechengService.selectPYkechengList(pYkecheng);
        ExcelUtil<PYkecheng> util = new ExcelUtil<PYkecheng>(PYkecheng.class);
        util.exportExcel(response, list, "课程列数据");
    }

    /**
     * 获取课程列详细信息
     */
    @PreAuthorize("@ss.hasPermi('hatha:ykecheng:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(pYkechengService.selectPYkechengById(id));
    }

    /**
     * 新增课程列
     */
    @PreAuthorize("@ss.hasPermi('hatha:ykecheng:add')")
    @Log(title = "课程列", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PYkecheng pYkecheng)
    {
        return toAjax(pYkechengService.insertPYkecheng(pYkecheng));
    }

    /**
     * 修改课程列
     */
    @PreAuthorize("@ss.hasPermi('hatha:ykecheng:edit')")
    @Log(title = "课程列", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PYkecheng pYkecheng)
    {
        return toAjax(pYkechengService.updatePYkecheng(pYkecheng));
    }

    /**
     * 删除课程列
     */
    @PreAuthorize("@ss.hasPermi('hatha:ykecheng:remove')")
    @Log(title = "课程列", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(pYkechengService.deletePYkechengByIds(ids));
    }
}
